package gw.com.android.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import www.com.library.app.Logger;
import www.com.library.util.DoubleConverter;

/**
 * Created by reeta.zou on 2016/11/29.
 * 消息推送处理类
 */
public class RemoteMessageReceiver extends BroadcastReceiver {

    private Context mContext;
    private String mCustomContent;

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        Bundle bundle = intent.getExtras();
        Logger.e("[MyReceiver] onReceive - " + intent.getAction());

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Logger.e( "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Logger.e("[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
//            if(!AppMain.isAppRun()) {
//                mCustomContent = bundle.getString(JPushInterface.EXTRA_EXTRA);
//                Logger.e( "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId +", " + mCustomContent);
//                processMessage();
//            }

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            mCustomContent = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if(!AppMain.isAppRun()) {
                Logger.e( "[MyReceiver] 用户点击打开了通知: " + mCustomContent);
                clickNotification();
            }


        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Logger.e( "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

        } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Logger.e("[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
        } else {
            Logger.e( "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

    /**
     * 发送通知
     */
    public void clickNotification(){
        if (mCustomContent != null && mCustomContent.length() > 1) {
            try {
                JSONObject obj = new JSONObject(mCustomContent);
                String contenttype2 = AppContances.BULLENTIN_TYPE_IMPORTANT+"";
                String mDataId = "";
                //用来判读是否使用新的openApiPush
                boolean isOpenApiPush = false;
                if (!obj.isNull("dataid")) {
                    mDataId = obj.getString("dataid");
                }else{
                    isOpenApiPush = true;
                    mDataId = String.valueOf(obj.getInt("id"));
                }
                String language = GTConfig.LANG_ZH_CN;
                if (!obj.isNull("lang")) {
                    language = obj.getString("lang");
                }

                //处理具体点击事件
                processCustomMessage(DoubleConverter.toIntData(mDataId),contenttype2, language,isOpenApiPush);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 处理具体点击事件
     * @param dataid
     * @param contenttype2
     */
    private void processCustomMessage(int dataid, String contenttype2, String language, boolean isOpenApiPush) {
        Logger.e("从通知栏启动应用程序。。。");
        Intent intent = mContext.getPackageManager().
                getLaunchIntentForPackage(AppMain.getApp().getPackageName());
        intent.setFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        //未开启应用处理
        intent.putExtra("where", AppContances.TYPE_FORM_NOTIFY);
        if(ConfigUtil.instance().homeFucType() != 0){
            intent.putExtra("mCurTag", ConfigType.TAB_HOME_TAG);
        }else{
            intent.putExtra("mCurTag", ConfigType.TAB_QUOTE_TAG);
        }
        intent.putExtra("mDataId", dataid);
        intent.putExtra("type",contenttype2);
        intent.putExtra("language", language);
        intent.putExtra("isOpenApiPush", isOpenApiPush);
        mContext.startActivity(intent);
    }

}
